﻿Imports VendasBLL
Imports VendasDAL
Public Class FuncionarioUC
    Dim ObjBLL As New FuncionarioBLL


#Region "Carrega grid"
    Sub CarregaGrid()
        DGVDados.DataSource = ObjBLL.buscar("", 7)
        DGVDados.Refresh()
    End Sub
#End Region

#Region "Novo"
    Private Sub TSBNovo_Click(sender As System.Object, e As System.EventArgs) Handles TSBNovo.Click
        Try
            FuncionarioFRM.Op = "I"
            TSBNovo.BackColor = Color.Gainsboro
            FuncionarioFRM.ShowDialog()
            CarregaGrid()
        Catch ex As Exception
        Finally
            TSBNovo.BackColor = Color.Transparent
        End Try

    End Sub
#End Region

#Region "Load"
    Private Sub FuncionarioUC_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        CarregaGrid()
        PLocalizar.Visible = False
        'Determinar o começo o filtro como descrição
        CBFiltro.SelectedIndex = 1
        CBFiltro.Focus()
    End Sub
#End Region

#Region "Cor de fundo do datagridview"
    Private Sub DGVDados_RowPrePaint(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DGVDados.RowPrePaint
        If DGVDados.Rows(e.RowIndex).Index Mod (2) = 1 Then
            DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region

#Region "Alterar"
    Private Sub TSBAlterar_Click(sender As System.Object, e As System.EventArgs) Handles TSBAlterar.Click

        Dim indice As Integer = -1
        Try
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("fun_codigo") Is Nothing) Then
                    indice = linha.Index
                End If
            Next
            If (indice <> -1) Then
                FuncionarioFRM.codigo = Val(DGVDados.Rows(indice).Cells("fun_codigo").Value)
                FuncionarioFRM.Op = "A"
                TSBAlterar.BackColor = Color.Gainsboro
                FuncionarioFRM.ShowDialog()
                CarregaGrid()

            Else
                MessageBox.Show("Selecione o funcionário a ser realizado á alteração", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            TSBAlterar.BackColor = Color.Transparent
        End Try
    End Sub
#End Region

#Region "Excluir"
    Private Sub TSBExcluir_Click(sender As System.Object, e As System.EventArgs) Handles TSBExcluir.Click
        Try
            Dim cod As Integer = 0
            Dim indice As Integer = -1
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("fun_codigo") Is Nothing) Then
                    ObjBLL = New FuncionarioBLL(Val(DGVDados.Rows(linha.Index).Cells("fun_codigo").Value.ToString()))
                End If
            Next
            If (ObjBLL.Codigo > 0) Then
                TSBExcluir.BackColor = Color.Gainsboro

                If MessageBox.Show("Confirma exclusão do funcionario " & ObjBLL.Nome & " ?", "", MessageBoxButtons.YesNo) = vbYes Then
                    ObjBLL.Status = 3
                    ObjBLL.InserirAlterar()
                    CarregaGrid()
                End If
            Else
                MessageBox.Show("Selecione o funcionario a ser excluido", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            TSBExcluir.BackColor = Color.Transparent

        End Try
    End Sub
#End Region

#Region "Localizar"
    Private Sub TSBLocalizar_Click(sender As System.Object, e As System.EventArgs) Handles TSBLocalizar.Click
        If PLocalizar.Visible = True Then
            PLocalizar.Visible = False
            TXTLocalizar.Text = ""
            TSBLocalizar.BackColor = Color.Transparent
        Else
            PLocalizar.Visible = True
            TXTLocalizar.Text = ""
            TSBLocalizar.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region

#Region "ComboBox Filtro"
    Private Sub CBFiltro_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles CBFiltro.KeyPress
        e.Handled = True
    End Sub
#End Region

#Region "Localizar no textbox"
    Private Sub TXTLocalizar_TextChanged(sender As System.Object, e As System.EventArgs) Handles TXTLocalizar.TextChanged
        Select Case CBFiltro.Text
            Case "Código"
                DGVDados.DataSource = ObjBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "Nome"
                DGVDados.DataSource = ObjBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "CPF"
                DGVDados.DataSource = ObjBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "Núm. Carteira"
                DGVDados.DataSource = ObjBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
           
        End Select
        If TXTLocalizar.Text = "" Then
            DGVDados.DataSource = ObjBLL.buscar("", 7)
        End If
        DGVDados.Refresh()
    End Sub

#End Region

#Region "Voltar"
    Private Sub TSBVoltar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBVoltar.Click
        statustabvendas = "N"
        Dispose()
    End Sub
#End Region

#Region "Keypress Localizar"
    Private Sub TXTLocalizar_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTLocalizar.KeyPress, TXTLocalizar.KeyPress
        If CBFiltro.SelectedIndex = 0 Or CBFiltro.SelectedIndex = 2 Or CBFiltro.SelectedIndex = 3 Then
            If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
                e.Handled = True
            End If
        End If
    End Sub
#End Region

#Region "ComboboxFiltro SelectedIndex"
    Private Sub CBFiltro_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBFiltro.SelectedIndexChanged
        If CBFiltro.SelectedIndex = 4 Then
            CBStatus.Visible = True
            TXTLocalizar.Visible = False
            CBStatus.SelectedIndex = 0
        Else
            CBStatus.Visible = False
            TXTLocalizar.Visible = True
        End If
        TXTLocalizar.Text = ""
        CarregaGrid()
    End Sub
#End Region

#Region "ComboboxStatus Selectedindex"
    Private Sub CBStatus_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBStatus.SelectedIndexChanged
        DGVDados.DataSource = ObjBLL.buscar(CBStatus.Text, CBFiltro.SelectedIndex)
    End Sub
#End Region

#Region "Relatorio"
    Public Sub gerarelatrio(titulo As String, formula As String)
        Try
            Dim frmrecursos As New RelatoriosVendaFRM
            Dim rpt As New FuncionarioDetalhadoREL

            Try
                rpt.SetDatabaseLogon(usuario, senha)
                rpt.SetParameterValue(0, titulo)
                rpt.RecordSelectionFormula = formula
                frmrecursos.CRVRelatorio.ShowGroupTreeButton = False
                frmrecursos.CRVRelatorio.ReportSource = rpt
                frmrecursos.ShowDialog()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
            frmrecursos.Dispose()
            rpt = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Finally

        End Try
    End Sub
#End Region

    Private Sub TSBDetalhes_Click(sender As System.Object, e As System.EventArgs) Handles TSBDetalhes.Click
        For Each linha As DataGridViewRow In DGVDados.SelectedRows
            If (Not linha.Cells("fun_codigo") Is Nothing) Then
                ObjBLL = New FuncionarioBLL(Val(DGVDados.Rows(linha.Index).Cells("fun_codigo").Value.ToString()))
            End If
        Next
        If (Not IsNothing(ObjBLL)) Then
            If (ObjBLL.Codigo > 0) Then gerarelatrio("Funcionário: " & ObjBLL.Nome, "{Ofi_funcionario.fun_codigo}=" & ObjBLL.Codigo)
        End If
    End Sub
End Class
